package com.finchina.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

/**
 * 作者：黄赛赛 on 2015/9/8 09:56
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class CustomTitleView extends View {

    /**
     * 文本
     */
    private String mTitleText;
    /**
     * 文本的颜色
     */
    private int mTitleTextColor;
    /**
     * 文本的大小
     */
    private int mTitleTextSize;

    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect mBound;
    private Paint mPaint;

    public CustomTitleView(Context context, AttributeSet attrs) {
//        super(context, attrs);
         this(context, attrs, 0);
    }


    public CustomTitleView(Context context) {
//        super(context);
        this(context, null);
    }


    public CustomTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        /**
         * 获得我们所定义的自定义样式属性
         */
        TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView,defStyleAttr, 0);

        mTitleText = ta.getString(R.styleable.CustomTitleView_titleText);
        mTitleTextColor = ta.getColor(R.styleable.CustomTitleView_titleColor, Color.BLACK);
        mTitleTextSize = ta.getDimensionPixelSize(R.styleable.CustomTitleView_titleSize,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
        ta.recycle();


        /**
         * 获得绘制文本的宽和高
         */
        mPaint = new Paint();
        mPaint.setTextSize(mTitleTextSize);
         mPaint.setColor(mTitleTextColor);

        mBound = new Rect();
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);

    }

/*    EXACTLY：一般是设置了明确的值或者是MATCH_PARENT

    AT_MOST：表示子布局限制在一个最大值内，一般为WARP_CONTENT

    UNSPECIFIED：表示子布局想要多大就多大，很少使用*/
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width,height;
        if (widthMode == MeasureSpec.EXACTLY){
            width = widthSize;
        }else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textWidth = mBound.width();
            int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width = desired;
        }

        if (heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else {
            mPaint.setTextSize(mTitleTextSize);
            mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
            float textHeight = mBound.height();
            Log.e("getPaddingTop()  ", " = " +getPaddingTop() );
            Log.e("getPaddingBottom()  ", " = " +getPaddingBottom() );
            int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
            Log.e("desired()  ", " = " +desired);

            height = desired;
        }
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.YELLOW);
        Log.e("getMeasuredWidth()  ", " = " + getMeasuredWidth());
        Log.e("getMeasuredHeight()  ", " = " + getMeasuredHeight());
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

        mPaint.setColor(mTitleTextColor);
        Log.e("getPaddingTop()  ", " = " + getPaddingTop());
        canvas.drawText(mTitleText, getPaddingLeft(), getPaddingTop() + 15, mPaint);
    }
}
